using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class dleState : IState
{
    private FSM manager;
    private Parameter parameter;

    public dleState(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }
    public void OnEnter()
    {
        parameter.animator.Play("BossIdie");
        manager.GetComponent<SpriteRenderer>().color = new Color(70f / 255, 70f / 255, 70f / 255, 255f / 255);
    }

    public void OnExit()
    {
        
    }

    public void OnUpdate()
    {
        //Debug.Log(1);
    }
}

public class runState : IState
{
    private FSM manager;
    private Parameter parameter;
    private float timer;//计时器
    
    public runState(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }
    public void OnEnter()
    {
        parameter.animator.Play("BossRun");
        parameter.getHit = true;
    }

    public void OnExit()
    {
        parameter.getHit = false;
        timer = 0;
        if (parameter.patrolPosition==1) { 
        manager.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
         }
    }

    public void OnUpdate()
    {
        //manager.FlipTo1();
        //manager.transform.position = Vector2.MoveTowards(manager.transform.position,
        //parameter.patrolPoints[patrolPosition].position, parameter.moveSpeed * Time.deltaTime);
        //Debug.Log(1);
        timer += Time.fixedDeltaTime;
        manager.FlipTo1();
        if (timer >= parameter.idleTime)
        {
            manager.TransitionState(StateType.bosskrun);
        }
    }
}

public class krunState : IState
{
    private FSM manager;
    private Parameter parameter;
    //private int patrolPosition = 1;//下标查找和切换巡逻点
    public krunState(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }
    public void OnEnter()
    {
        parameter.animator.Play("bosskrun");
    }

    public void OnExit()
    {
        if (parameter.target == null&&!GameManager.gameManager_Instance.bossbool) { 
            parameter.patrolPosition++;

        if (parameter.patrolPosition >= parameter.patrolPoints.Length)
        {
            parameter.patrolPosition = 0;
        }
         }
    }

    public void OnUpdate()
    {
        manager.transform.position = Vector2.MoveTowards(manager.transform.position,
            parameter.patrolPoints[parameter.patrolPosition].position, parameter.moveSpeed * Time.deltaTime);
        //if (parameter.target!=null) {Debug.Log(parameter.target.position); }
        
        if (parameter.target != null) 
        {
            manager.TransitionState(StateType.ChaseState);
        }
        if (GameManager.gameManager_Instance.bossbool) 
        {
            manager.TransitionState(StateType.ChaseStates);
        }
        if (Vector2.Distance(manager.transform.position, parameter.patrolPoints[parameter.patrolPosition].position) < .1f)
        {
            manager.TransitionState(StateType.BossRun);
        }
    }
}

public class ChaseState : IState//追击
{
    private FSM manager;
    private Parameter parameter;
    public Vector3 en = Vector3.zero;
    public ChaseState(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }
    public void OnEnter()
    {
        parameter.animator.Play("bosskrun");
        en = manager.transform.position;
    }

    public void OnUpdate()
    {
        gun.gun_Instance.fire = true;
        if (parameter.target != null) 
        { 
            //Debug.Log(parameter.target.position); 
        }
        if (GameManager.gameManager_Instance.bossbool)
        {
            manager.TransitionState(StateType.ChaseStates);
        }
        if (parameter.target)
        {
            if (parameter.patrolPosition == 1 || parameter.patrolPosition == 3) { 
            en.x = parameter.target.position.x;
            manager.transform.position = Vector2.MoveTowards(manager.transform.position,
          en, parameter.moveSpeed * Time.deltaTime);
        } else {
                en.y = parameter.target.position.y;
                manager.transform.position = Vector2.MoveTowards(manager.transform.position,
              en, parameter.moveSpeed * Time.deltaTime);
            }
        }
        if (parameter.target == null)
        {
            gun.gun_Instance.fire = false;
            manager.TransitionState(StateType.bosskrun);
        }
    }

    public void OnExit()
    {

    }
}

public class ChaseStates : IState//攻击
{
    private FSM manager;
    private Parameter parameter;
    public Vector3 en = Vector3.zero;
    public ChaseStates(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }
    public void OnEnter()
    {
        parameter.animator.Play("bosskrun");
        en = manager.transform.position;
    }

    public void OnUpdate()
    {
        gun.gun_Instance.fire = true;
        
        if (Player.Player_Instance!=null)
        {
            if (parameter.patrolPosition == 1 || parameter.patrolPosition == 3)
            {
                en.x = Player.Player_Instance.transform.localPosition.x;
                manager.transform.position = Vector2.MoveTowards(manager.transform.position,
              en, parameter.moveSpeed * Time.deltaTime);
            }
            else
            {
                en.y = Player.Player_Instance.transform.localPosition.y;
                manager.transform.position = Vector2.MoveTowards(manager.transform.position,
              en, parameter.moveSpeed * Time.deltaTime);
            }
        }
        
    }

    public void OnExit()
    {
        gun.gun_Instance.fire = false;
    }
}